//
//  DesciptionVC.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/19/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class DesciptionViewController: UIViewController {
    
    // MARK: - Outlets


     private lazy var submitBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 15
        btn.setTitle("Готово", for: .normal)
        btn.backgroundColor = .blueColor
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel!.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        btn.tag = 1
        return btn
    }()
    
    private lazy var skipBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 15
        btn.setTitle("Пропустить", for: .normal)
        btn.backgroundColor = .white
        btn.tag = 2

        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.blueColor.cgColor

        btn.setTitleColor(.blueColor, for: .normal)
        btn.titleLabel!.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        return btn
    }()

    private lazy var textView: UITextView = {
        let textView = UITextView()
        textView.backgroundColor = .whiteBlueColor
        textView.layer.cornerRadius = 10
        textView.clipsToBounds = true
        textView.textContainerInset = UIEdgeInsets(top: 20, left: 16, bottom: 20, right: 16)
        textView.textColor = .lightGray
        textView.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        textView.delegate = self
        textView.text = placeholderTxt
        return textView
    }()
    
    private lazy var onlyWeToggle = ToggleView()
    
    private lazy var warnTxt: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .red
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        lbl.text = "Не указывайте свои данные и не предлагайте услуги здесь, иначе объявление может не пройти модерацию."
        lbl.sizeToFit()
        lbl.numberOfLines = 0
        return lbl
    }()
    private lazy var infoTxt: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .black
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        lbl.text = "Вы подтверждаете, что объявление размещается только на Auto Nomad.\n\nЧерез некоторое время ваше объявление может быть отмечено специальным значком."
        lbl.sizeToFit()
        lbl.numberOfLines = 0
        return lbl
    }()

    // MARK: - Props

    public weak var delegate: AdCreatorProtocol!
    
    let placeholderTxt = "Опишите достоинства и недостатки своего автомобиля"
    
    // MARK: - UI Actions
    
    @objc private func goBack (sender: UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func closeVC (sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true, completion: nil)
        delegate.removeEverything()
    }
    
    @objc private func screenTap(sender: UITapGestureRecognizer) {
        self.textView.resignFirstResponder()
    }

    @objc private func goNext (sender: UIButton) {
        let nextVC = NumberViewController()
        nextVC.delegate = self.delegate

        let text = placeholderTxt == self.textView.text || sender.tag == 2 ? "" : self.textView.text

        delegate.getDescriptions(text: text, onlyAutoNomad: onlyWeToggle.isOn())
        
        self.navigationController?.pushViewController(nextVC, animated: true)
    }
    
    
    // MARK: - Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpUI()
        self.setUpNavBar()
    }

}

// MARK: - Methods


extension ImageSelectVC {
    
}

// MAKR: - UI

extension DesciptionViewController {
    
    private func setUpUI () {
        view.backgroundColor = .white
        self.view.addSubview(textView)
        self.view.addSubview(onlyWeToggle)
        
        self.view.addSubview(warnTxt)
        self.view.addSubview(infoTxt)
        
        self.view.addSubview(submitBtn)
        self.view.addSubview(skipBtn)
        self.addConstraints()
        
        submitBtn.addTarget(self, action: #selector(goNext(sender:)), for: .touchUpInside)
        skipBtn.addTarget(self, action: #selector(goNext(sender:)), for: .touchUpInside)
        
        let keyboardTap = UITapGestureRecognizer(target: self, action: #selector(screenTap(sender:)))
        self.view.addGestureRecognizer(keyboardTap)
        
        onlyWeToggle.makeToggleView(name: "Авто только в AutoNomad", isOn: false)
        
        self.addCloseIcon()
    }
    
    private func addConstraints () {
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(30)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(114)
        }
        warnTxt.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(textView.snp.bottom).offset(8)
        }
        onlyWeToggle.snp.makeConstraints { (make) in
            make.top.equalTo(warnTxt.snp.bottom).offset(24)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(64)
        }
        infoTxt.snp.makeConstraints { (make) in
            make.top.equalTo(onlyWeToggle.snp.bottom).offset(8)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        skipBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-16)
            make.height.equalTo(44)
        }
        submitBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(skipBtn.snp.top).offset(-8)
            make.height.equalTo(44)
        }
    }
    
    private func setUpNavBar () {
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, landscapeImagePhone: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        
        self.navigationItem.simpleNavBar(title: "Описание", backButton: backButton)
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .bold)
        ]
        navigationItem.hidesSearchBarWhenScrolling = false
        self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    }
    
    private func addCloseIcon () {
        let closeBarBnt = UIBarButtonItem(image: UIImage(named: "dismiss"), style: .plain, target: self, action: #selector(closeVC))
        closeBarBnt.tintColor = .greyColor
        self.navigationItem.rightBarButtonItem = closeBarBnt
    }
}


extension DesciptionViewController: UITextViewDelegate {
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.textView.textColor = .black
        if self.textView.text == placeholderTxt {
            self.textView.text = ""
        }

    }
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        self.textView.textColor = .black
        if self.textView.text == placeholderTxt {
            self.textView.text = ""
        }
        return true
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            self.addPlaceholder()
        }
    }
    
    
    private func addPlaceholder () {
        self.textView.text = placeholderTxt
        self.textView.textColor = .lightGray
    }
}


